<%--
  Created by IntelliJ IDEA.
  User: leixiaolong
  Date: 2018/9/13
  Time: 下午6:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/include.jsp" %>

<html>
<head>
    <title>入库单管理</title>
    <%--<link rel="stylesheet" href="/static/css/rpt-condition.css" media="all" type="text/css">--%>
    <%--<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/layui/css/layui.css" media="all"--%>
    <%--type="text/css">--%>
    <style>
        .layui-table-header {
            background-color: #1E9FFF;
        }

        .layui-table thead tr {
            background-color: #1E9FFF;
            color: black;
        }
    </style>
</head>
<body>
<div style="padding: 10px;">
    <div class="layui-form" style="border-color: black; border-style: solid; border-width: 1px;">
        <div style="height: 20%; margin-top: 15px;">
            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">仓库名称：</label>
                <div class="layui-input-inline">
                    <select class="layui-form-select" id="sel_stor">
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">入库日期：</label>
                <input type="text" class="layui-input-inline dom-input" id="date_stockin"
                       placeholder="yyyy-MM-dd">
            </div>
            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">机构名称：</label>
                <div class="layui-input-inline">
                    <select class="layui-form-select" id="sel_organ">
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">入库类型：</label>
                <div class="layui-input-inline">
                    <select class="layui-form-select" id="sel_stockin_type">
                    </select>
                </div>
            </div>

            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">单据状态：</label>
                <div class="layui-input-inline">
                    <select class="layui-form-select" id="sel_stat">
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">审核状态：</label>
                <div class="layui-input-inline">
                    <select class="layui-form-select" id="sel_verfy_stat">
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item" style="float: right; margin-right: 6%; margin-top: -50px;">
            <button class="layui-btn layui-btn-normal layui-btn-radius" lay-submit data-type="search">搜索</button>
            <button class="layui-btn layui-btn-warm layui-btn-radius" style="margin-right: 20px;"
                    lay-submit data-type="reset">重置
            </button>
        </div>
        <div style="clear:both"></div>
    </div>

    <div class="layui-btn-group" style="float: right; margin-right: 100px; margin-top: 5px;">
        <button class="layui-btn layui-btn-sm layui-btn-primary" lay-submit data-type="add">新增</button>
        <button class="layui-btn layui-btn-sm layui-btn-primary">导出</button>
    </div>


    <div style=" background-color: white; margin-top: 35px;">
        <table class="layui-hide" id="tab_data_content"></table>
    </div>
</div>
</body>
</html>

<script type="text/html" id="rowBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script language='javascript' src="${pageContext.request.contextPath}/plugins/layui/layui.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-select.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-date.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/utils/moment.js"></script>

<script>
    layui.use(["form", "element", "jquery", "layer", "laydate", "table"], function () {
        var form = layui.form, table = layui.table, element = layui.element, layer = layui.layer, $ = layui.jquery;

        var nowdate = moment().format('YYYY-MM-DD');
        renderDate('date_stockin', nowdate);

        initStorSelect("sel_stor");
        var params = {};
        params["sysCode"] = 22;
        initCodeTableSelect('sel_stockin_type', "/base/getSysCodeList", params);
        initOrganSelect('sel_organ', null);

        params = {};
        params["sysCode"] = 14;
        initCodeTableSelect("sel_stat", "/base/getSysCodeList", params);
        initCodeTableSelect("sel_verfy_stat", "/base/getVerfyStatList", null);

        var tableIns = table.render({
            id: 'tab_data_content',
            elem: '#tab_data_content',
            url: '/stor/stockin/getStockinPager',
            type: 'GET',
            where: {
                storCode: $('#sel_stor').find('option:selected').val(),
                stockDate: $('#date_stockin').val(),
                stockType: $('#sel_stockin_type').find('option:selected').val(),
                organCode: $('#sel_organ').find('option:selected').val(),
                stat: $('#sel_stat').find('option:selected').val(),
                verfyStat: $('#sel_verfy_stat').find('option:selected').val()
            },
            cellMinWidth: 90,
            height: 'full-230',
            cols: [[
                {field: 'seqNo', title: '序号', type: 'numbers', align: 'center'},
                {field: 'stockId', title: '入库单id', align: 'center'},
                {field: 'storId', title: '仓库id', align: 'center', hide: true},
                {
                    field: 'storName', title: '仓库名称', align: 'center',
                    templet: function (rowdata) {
                        if (typeof(rowdata.storName) === 'undefined' || null == rowdata.storName) {
                            return '-';
                        }
                        return rowdata.storName;
                    }
                },
                {field: 'stockType', title: '入库单类型代码', align: 'center', hide: true},
                {
                    field: 'stockTypeName', title: '入库单类型', align: 'center', templet: function (rowdata) {
                        switch (rowdata.stockType) {
                            case 1:
                                return '采购入库';
                            case 2:
                                return '调入入库';
                            case 3:
                                return '借入入库';
                            case 4:
                                return '归还入库';
                            case 5:
                                return '组装入库';
                            default:
                                return rowdata.stockType;
                        }
                    }
                },
                {
                    field: 'stockDate', title: '入库日期', align: 'center', minWidth: 170,
                    templet: function (rowdata) {
                        return new moment(rowdata.stockDate).format('YYYY-MM-DD');
                    }
                },
                {field: 'organCode', title: '机构id', align: 'center', hide: true},
                {field: 'organName', title: '机构名称', align: 'center'},
                {field: 'makerId', title: '制单人id', align: 'center', hide: true},
                {field: 'makerName', title: '制单人', align: 'center'},
                {
                    field: 'makeDate', title: '制单日期', align: 'center', minWidth: 170,
                    templet: function (rowdata) {
                        return new moment(rowdata.makeDate).format('YYYY-MM-DD HH:mm:ss');
                    }
                },
                {
                    field: 'exchDate', title: '交易日期', align: 'center',
                    templet: function (rowdata) {
                        return new moment(rowdata.exchDate).format('YYYY-MM-DD');
                    }
                },
                {
                    field: 'digest', title: '摘要', align: 'center',
                    templet: function (row) {
                        if (typeof(row.digest) === 'undefined' || null == row.digest) {
                            return '-';
                        }
                        return row.digest;
                    }
                },
                {
                    field: 'stat', title: '状态', align: 'center',
                    templet: function (row) {
                        switch (row.stat) {
                            case 0:
                                return '正常';
                            case 1:
                                return '已删除';
                            default:
                                return row.purcType;
                        }
                    }
                },
                {
                    field: 'verfyStat', title: '审核状态', align: 'center',
                    templet: function (row) {
                        switch (row.verfyStat) {
                            case 0:
                                return '待提交审核';
                            case 1:
                                return '待审核';
                            case 2:
                                return '审核通过';
                            case 3:
                                return '审核未通过';
                            case 4:
                                return '反审核';
                            default:
                                return row.verfyStat;
                        }
                    }
                },
                {field: 'verfyErrMsg', title: '审核失败原因', align: 'center'},
                {
                    field: 'opertaor', title: '操作', align: 'center', templet: '#rowBar', fixed: 'right'
                }
            ]],
            done: function () {
                // var myDate = new Date();
                // layer.msg("数据渲染完成, date:" + myDate.getSeconds());
            },
            page: {
                limit: 20,
                limits: [10, 20, 50]
            }
        });

        var actives = {
            search: function () {
                reloadTable();
            },
            add: function () {
                // layer.msg("打开新增页面");
                layer.open({
                    type: 2,
                    title: ['入库单录入', 'text-align: center; font-size: 20px;'],
                    area: [winContSize.width, winContSize.height],
                    offset: 'auto',
                    content: '/stor/stor_stockin_add'
                });
            },
            reset: function () {
                var nowdate = moment().format('YYYY-MM-DD');
                renderDate('date_stockin', nowdate);

                $('#sel_stor').val('');
                $('#sel_stockin_type').val('');
                $('#sel_organ').val('');
                $('#sel_stat').val('');
                $('#sel_verfy_stat').val('');

                form.render();
            }
        };

        form.on('submit', function (data) {
            actives[$(this).data("type")].call(this);
        });

        /**
         * 监听表行事件
         */
        table.on('tool', function (obj) {
            //获得当前行数据
            var rowdata = obj.data;
            var layEvent = obj.event;//获得当前行事件
            var tr = obj.tr;//获得当前行tr的dom对象

            if (layEvent == 'edit') {//编辑
                debugger
                const index = layer.open({
                    type: 2,
                    title: ['入库单编辑', 'text-align: center; font-size: 20px;'],
                    area: ['94%', '90%'],
                    offset: 'auto',
                    content: '/stor/stor_stockin_edit',
                    success: function (layero, index) {
                        var iframe = window[layero.find('iframe')[0]['name']];
                        iframe.syncInit(rowdata);
                    }
                });
            } else if (layEvent == 'del') {//删除
                var wheredata = {stockId: rowdata.stockId}

                layer.confirm("是否删除本条记录", {icon: 3}, function (index) {
                    //联动调用后台删除入库单
                    $.ajax({
                        url: '/stor/stockin/delStockin',
                        type: 'POST',
                        // contentType: 'application/json',
                        data: {stockId: rowdata.stockId},
                        async: false,
                        success: function (result) {
                            layer.msg("删除入库单完成");
                            reloadTable();
                        },
                        error: function (error) {
                            layer.msg('删除入库单异常', error.toString());
                        }
                    });
                    // obj.del();
                    // layer.close(index);
                });
            } else {//其他暂不支持
                layer.msg("请等待开放功能");
            }

            //同步更新缓存对应的值
            // obj.update({username:'123', title: 'xxx'});
        });

        /**
         * 监听表行双击事件
         */
        table.on('rowDouble', function (obj) {
            var rowData = obj.data;

            var index = layer.open({
                type: 2,
                title: ['入库单详情', 'text-align: center; font-size: 20px;'],
                area: [winContSize.width, winContSize.height],
                offset: 'auto',
                content: '/stor/stor_stockin_receipts',
                success: function (layero, index) {
                    var iframe = window[layero.find('iframe')[0]['name']];
                    iframe.syncInit(rowData);
                }
            });
        });
    });

    function reloadTable() {
        layui.use(['table', 'jquery'], function () {
            var table = layui.table, $ = layui.jquery;

            table.reload('tab_data_content', {
                where: {
                    storCode: $('#sel_stor').find('option:selected').val(),
                    stockDate: $('#date_stockin').val(),
                    stockType: $('#sel_stockin_type').find('option:selected').val(),
                    // organCode: $('#sel_organ').find('option:selected').val(),
                    stat: $('#sel_stat').find('option:selected').val(),
                    verfyStat: $('#sel_verfy_stat').find('option:selected').val()
                }
            });
        })
    }
</script>
